package com.rural.bus;

import org.json.JSONObject;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.rural.bus.model.User;
import com.rural.bus.net.MyHttpUtil;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.net.Url;
import com.rural.bus.utils.DialogUtil;
import com.rural.bus.utils.ProgressUtil;
import com.rural.bus.utils.VersionUtil;
import com.rural.bus.view.CommonAlertDialog;

public class SettingActivity extends BaseActivity {

	private TextView tvAbout;
	private TextView tvFeedback,tv_check_version,tvModifyPasswd;
	private Button btLogout;
	private TextView tvResetPsw;
	CommonAlertDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);

		initView();
		setListeners();
	}
	
	@Override
	protected void initView() {
		tvAbout = (TextView) findViewById(R.id.tvAbout);
		tvFeedback=(TextView) findViewById(R.id.feedback);
		tvResetPsw=(TextView) findViewById(R.id.tvResetPsw);
		tv_check_version = (TextView) findViewById(R.id.tv_check_version);
		
		tvModifyPasswd=(TextView) findViewById(R.id.tv_modifypasswd);

		btLogout = (Button) findViewById(R.id.logout);
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));
		mBtMiddle.setText("设置");

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);
		
	}

	@Override
	protected void initData() {

	}

	protected void setListeners() {
		
		tvModifyPasswd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivity(ModifyPasswordActivity.class);
			}
		});
		
		tvResetPsw.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				startActivity(ResetPswActivity.class);
			}
		});
		tvAbout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(AboutActivity.class);
			}
		});
		
		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		tvFeedback.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivity(FeedbackActivity.class);
			}
		});

		btLogout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogUtil.dialog(SettingActivity.this, "确认退出吗？", "确认", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						
						User.logout(SettingActivity.this);
						dialog.dismiss();
						finish();
					}
				}, "取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
					}
				});
				
			}
		});
		
		tv_check_version.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				checkUpdate();
				
			}
		});
	}

	
	private void checkUpdate() {
		MyHttpUtil http = new MyHttpUtil();
		ProgressUtil.showProgress(this, "");
		http.send(Url.version_update, new OnNetCallback() {

			@Override
			public void success(JSONObject object) {
				checkVersion(object);
				ProgressUtil.closeProgress();
			}

			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
			}
		});
	}

	private void checkVersion(JSONObject object) {
		// "version": "20151201",
		// "url":
		// "http://club.autohome.com.cn/bbs/thread-c-3286-45161297-1.html",
		// "remarks": "升级了版本功能，麻烦升级哦！"

		try {
			int currentVersion = VersionUtil.getVersionCode(getApplicationContext());
			int version = object.optInt("version", 1);
			final String url = object.optString("url", "");
			if (version > currentVersion) {
				if (dialog == null) {
					dialog = new CommonAlertDialog(this, "更新", "取消", "有新版本来咯...", new CommonAlertDialog.OnDialogDismissListener() {
						@Override
						public void onConfirmClicked() {
							if (!TextUtils.isEmpty(url)) {
								Uri uri = Uri.parse(url);

								Intent intent = new Intent(Intent.ACTION_VIEW, uri);
								intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								MyApplication.instance().startActivity(intent);
							}
						}

						@Override
						public void onCancelClicked() {

						}
					});
					dialog.setOwnerActivity(this);
				}
				dialog.show();
			} else {
				toast("已经是最新版本");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}